package 富婆梦.第一阶段.第七周IO流;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第七周
 * @ClassName: ChannelCopy
 * @Author: hemengjin
 * @Description: nio：一个通道同时读和写
 * @Date:2021/9/12 21:36
 * @Version: 1.0
 */
public class ChannelCopy {
    private static final int BSIZE = 1024;

    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("argument:sourcefile destfile");
            System.exit(1);
        }
        try {
            FileChannel
                    in = new FileInputStream(args[0]).getChannel(),
                    out = new FileOutputStream(args[1]).getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
            while (in.read(buffer) != -1) {
                buffer.flip();//准备开始写入
                out.write(buffer);
                buffer.clear();//准备读
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
